package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"
)

type ChartOfAccountAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewChartOfAccountAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChartOfAccountAddLogic {
	return &ChartOfAccountAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// ChartOfAccountAdd 创建资产科目表项
func (l *ChartOfAccountAddLogic) ChartOfAccountAdd(in *property_service.ChartOfAccount) (*property_service.Response, error) {
	// 获取资产科目表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).ChartOfAccount
	// 创建一个新的资产科目表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.ChartOfAccount{
		AssetID:                  in.AssetID,                  // 资产ID
		FixedAssetSubjectsID:     in.FixedAssetSubjectsID,     // 固定资产科目ID
		FixedAssetSubjectsName:   in.FixedAssetSubjectsName,   // 固定资产科目名称
		DepreciationSubjectsID:   in.DepreciationSubjectsID,   // 累计折旧科目ID
		DepreciationSubjectsName: in.DepreciationSubjectsName, // 累计折旧科目名称
		IsMultiSubject:           in.IsMultiSubject,           // 是否多科目
		SubjectID:                in.SubjectID,                // 科目ID
		SubjectName:              in.SubjectName,              // 科目名称
		UsageRoomID:              in.UsageRoomID,              // 使用科室ID
		UsageRoom:                in.UsageRoom,                // 使用科室名称
		Percentage:               in.Percentage,               //占比
		TenantID:                 in.TenantID,                 // 租户ID
		CreatedAtuser:            in.CreatedAtuser,            // 创建用户
		UpdatedAtuser:            in.UpdatedAtuser,            // 更新用户
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
